package com.example.qqfake;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.BufferedReader;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<User> items;
    private OnItemButtonClickListener listener;

    public MyAdapter(List<User> items, OnItemButtonClickListener listener) {
        this.items = items;
        this.listener =listener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_item, parent, false);
        return new MyAdapter.ItemViewHolder1(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ((MyAdapter.ItemViewHolder1) holder).bindData(items.get(position));
        // 为第一个按钮设置点击监听器
        ((ItemViewHolder1) holder).chat_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理第一个按钮的点击事件,跳转到对应的聊天对象
                Intent intent = new Intent(v.getContext(), ChatActivity.class);
                String id = Integer.toString(items.get(position).getId());
                intent.putExtra("chat_partner",id);
                intent.putExtra("chat_name",items.get(position).getUsername());
                v.getContext().startActivity(intent);

            }
        });
        // 设置第二个按钮的点击监听器
        ((ItemViewHolder1) holder).delete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 调用Activity实现的接口方法
                if (listener != null) {
                    listener.onItemButton2Click(position);
                }
            }
        });

    }

    public void setData(List<User> m){
        this.items.clear();
        this.items.addAll(m);

    }

    public void addItem(User user){
        items.add(user);
        notifyItemInserted(items.size() - 1);

    }


    @Override
    public int getItemCount() {
        return items.size();
    }

    public static class ItemViewHolder1 extends RecyclerView.ViewHolder {

        private TextView textView1;
        private TextView textView2;
        private Button chat_button;
        private Button delete_button;

        public ItemViewHolder1(View itemView) {
            super(itemView);

            textView1 = itemView.findViewById(R.id.friend_name);
            textView2 = itemView.findViewById(R.id.friend_id);
            chat_button = itemView.findViewById(R.id.goto_chat);
            delete_button = itemView.findViewById(R.id.delete);
        }

        public void bindData(User data) {

            textView1.setText(data.getUsername());
            String id = Integer.toString(data.getId());
            textView2.setText(id);

        }
    }

}
